import { uploadAttachment } from '@/services/util/util'
import { downloadUrl } from '@/script/util'

/**
 * Description: TODO 文件操作组件
 * */
export default class FileUtil {
  /**
   * Description: TODO 附件上传
   * param: fileList  上传的文件列表
   * param: scope 上传的文件范围
   * param: isSecure  上传文件是否受到保护
   * return:
   */
  static async uploadFile (fileList, scope, isSecure) {
    // 构建上传表单数据
    const formData = new FormData()
    fileList.forEach((val, index) => {
      formData.append('files', val.raw)
    })
    formData.append('scope', scope)
    formData.append('isSecure', isSecure)

    // 构建其他配置
    const config = this.buildConfig(fileList)
    // 上传文件
    return uploadAttachment(formData, config).then((res) => {
      return res
    })
  }

  /**
   * Description: TODO 构建上传配置
   * param: fileList  上传的文件列表
   * */
  static buildConfig (fileList) {
    // 上传进度条配置
    const config = {
      timeout: 120000, // 设置超时时长
      onUploadProgress: function (progress) {
        fileList.percent = Math.round(
          (progress.loaded * 80) / progress.total
        )
      }
    }
    return config
  }

  static getDownloadUrl (url) {
    return downloadUrl + url
  }
}
